package com.java.ttt.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CheckMoveDao {

	public String CheckMove(String userName, int gameID) {

		String move = null;
		

		try {
			Connection conn = BaseDbConnection.getConnection();
			ResultSet rSet = null;
			String sql = "select * from ttt3.GameState where gameID=? and playerID!=? and timestamp=(select max(timestamp) from ttt3.GameState where gameID=?)";
			
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, gameID);
			ps.setString(2, userName);
			ps.setInt(3, gameID);
			rSet = ps.executeQuery();
			if (rSet != null && rSet.next()) {
				move = rSet.getString(3);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return move;
	}
}
